package com.sckj.springbootdome.controller;

import com.sckj.springbootdome.entity.LoginUser;
import com.sckj.springbootdome.sevice.LoginService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class LoginController {
    @Autowired
    private LoginService loginService;

    @RequestMapping("/toLogin")
    public String toLogin() {
        return "login";
    }

    @PostMapping("/login")
    public String login(HttpServletRequest request, LoginUser user) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        user = loginService.login(username, password);
        if (user != null) {
            return "redirect:/list";
        } else {
            return "redirect:/toLogin";
        }
    }
    @GetMapping("/exit")
    public String exit (HttpServletRequest request){
        request.getSession().removeAttribute("user");
        return "redirect:/toLogin";
    }
}
